<?php

use wechat\assets\WechatEditorAsset;
use wechat\models\WeContent;
use yii\web\View;

/**
 * User: shutao
 * @var View $this
 * @var $model WeContent
 */
$this->title = '编辑';
WechatEditorAsset::register($this);
?>

<div>
    <form class="layui-form">

        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                <span class="x-red">*</span> <?php echo $model->getAttributeLabel('title') ?>
            </label>
            <div class="layui-input-inline">
                <input type="text" name="title" id="title" required="" lay-verify="required"
                       placeholder=""
                       value="<?php echo $model->title ?>"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="title" class="layui-form-label">
                <?php echo $model->getAttributeLabel('keyword') ?>
            </label>
            <div class="layui-input-inline">
                <input type="text" name="keyword" id="keyword"
                       placeholder=""
                       maxlength="30"
                       value="<?php echo $model->keyword ?>"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">请使用英文来作为索引词，最多30个字符</div>
        </div>

        <div class="layui-form-item">
            <label for="type" class="layui-form-label"> <?php echo $model->getAttributeLabel('type') ?>
            </label>
            <div class="layui-input-inline">
                <select name="type" id="type">
                    <option>请选择</option>
                    <?php foreach (WeContent::typeText() as $k => $text) { ?>
                        <option value="<?php echo $k ?>" <?php if ($model->type == $k) {
                            echo "selected";
                        } ?>><?php echo $text ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="content" class="layui-form-label"> <?php echo $model->getAttributeLabel('content') ?>
            </label>
            <div class="layui-input-block" style="z-index: 1;">
                <wechat-editor><?php echo $model->content ?></wechat-editor>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <input name="_csrf" type="hidden" id="_csrf" value="<?= \Yii::$app->request->csrfToken ?>">
            <?php if ($model->id) { ?>
                <input type="hidden" name="id" value="<?php echo $model->id ?>">
            <?php } ?>
            <button class="layui-btn" lay-filter='save' lay-submit="">保存</button>
        </div>
    </form>
</div>


<script>
    //Demo
    var qrcode = 0;

    function save(res) {
        qrcode = res.id
        document.getElementById('preview').innerHTML = '<div style="margin: 10px 0;"><img src="' + res.data + '" style="width: 150px" /></div>'
    }

    layui.use(['form', 'laydate', 'jquery'], function () {
        var form = layui.form, laydate = layui.laydate, $ = layui.$;

        //监听提交
        form.on('submit(save)', function (data) {
            $.ajax({
                type: "POST",
                url: '#',
                data: _.assign(data.field, {qrcode: qrcode}),
                success: function (response, textStatus, xhr) {
                    layer.msg('成功', {icon: 6, time: 1000}, function () {
                        xlayer.close(response);
                    });
                },
                error: function (xhr, textStatus, errorThrown) {
                    // 1 正确, 2 错误(X), 3 询问，4 锁定, 5 不开心, 5 开心(笑脸)
                    layer.alert(textStatus + ':' + xhr.responseText, {icon: 2}, function (index) {
                        layer.close(index);
                    });
                },
                dataType: 'json'
            });
            return false;
        });
    });
</script>

